const globalIp = getApp().globalData.userIp
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataColor: false,
    dataTime:"日期选择", 
    dataTime1:"起始日期",
    dataTime2:"截止日期",
    dataFlag: false, //控制综合下拉框是否显示
    nowTime: getApp().globalData.nowTime,
    info: '客户选择',
    infoValue: '',
    userCode: '',
    userColor: false,
    userFlag: false,//控制综合下拉框是否显示
    nameCall: '精确搜索',
    nameColor: false,
    nameFlag: false,
    nameCallInfo: '',

    pageIndex: 1,
    projectList: [],
  },

  getInputValue:function(e) {
    this.setData({
      nameCallInfo: e.detail.value
    })
  },

  selectData: function(){
    this.setData({
      nameFlag: false,
      nameColor: true,
      pageIndex: 1
    })
    this.getProjectTwo();
  },

  nameShow: function () {//综合下拉
    if (this.data.nameFlag){
      this.setData({
        nameFlag : !this.data.nameFlag,
        dataFlag: false,
        dataColor: false,
        nameColor: false
      })
    }else{
      this.setData({
        nameFlag: !this.data.nameFlag,
        dataFlag: false,
        dataColor: false,
        nameColor: true
      })
    }
  },

  dataShow: function () {//日期下拉
    if (this.data.dataFlag){
      this.setData({
        dataFlag : !this.data.dataFlag,
        nameFlag: false,
        nameColor: false
      })
    }else{
      this.setData({
        dataFlag: !this.data.dataFlag,
        nameFlag: false,
        nameColor: false
      })
    }
  },

  changeDate1(e) {
    this.setData({
      dataTime1: e.detail.value,
      dataColor: true
    })
  },
  changeDate2(e) {
    this.setData({
      dataTime2: e.detail.value,
      dataFlag: false,
      dataColor: true,
      pageIndex: 1
    })
    this.getProjectTwo()
  },

  getProjectTwo() {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: globalIp + 'home/ordr/orderDetail',
      data: {
        sdate: that.data.dataTime1 == '起始日期' ? '' : that.data.dataTime1,
        edate: that.data.dataTime2 == '截止日期' ? '' : that.data.dataTime2,
        // userName: '',
        // cardCode: '',
        value: that.data.nameCallInfo || '',
        pageIndex: that.data.pageIndex,
        openId: wx.getStorageSync('userInfo').openid || ''
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.jsonObject) {
          var data = res.data.jsonObject.data;
          if (data.length > 2) {
            var arr = [];
            var res = [];
            for (var i in data) {
              arr[i] = [];
              for(var j in data[i]) {
                arr[i].push(data[i][j]);
              }
            }
            if (arr.length > 0) {
              for (var i = 0; i < arr[0].length; ++i) {
                res[i] = [];
                for (var j = 0; j < arr.length; ++j) {
                  res[i][j] = arr[j][i];
                }
              }
              that.setData({
                projectList: res
              })
            }
          } else {
            that.setData({
              pageIndex: that.data.pageIndex - 1
            })
            wx.showToast({
              title: '无数据~',
              icon: 'error'
            })
          }
        } 
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {
        wx.hideLoading()
      }
    })
  },

  prevPage: function() {
    if (this.data.pageIndex === 1) {
      wx.showToast({
        title: '已经到达第一页~',
      })
    } else {
        this.setData({
          pageIndex: this.data.pageIndex - 1
        })
        this.getProjectTwo()
    }
  },

  nextPage: function() {
    this.setData({
      pageIndex: this.data.pageIndex + 1
    })
    this.getProjectTwo()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getProjectTwo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

})